<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">

      <el-form-item label="职位名称" prop="name">
        <el-input v-model="queryParams.title" placeholder="请输入职位名称" clearable size="small"
          @keyup.enter.native="handleQuery" />
      </el-form-item>

      <el-form-item label="截至时间" prop="endTime">
        <el-date-picker clearable size="small" v-model="queryParams.deadline" type="date" value-format="yyyy-MM-dd"
          placeholder="选择截至时间">
        </el-date-picker>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
          v-hasPermi="['hire:hire:add']">新增</el-button>
      </el-col>

      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="hireList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="职位名称" align="center" prop="title">
      </el-table-column>
      <el-table-column label="职位描述" align="center" prop="descInfo" show-overflow-tooltip />
      <el-table-column label="招聘人数" align="center" prop="hireNum">
      </el-table-column>
      <el-table-column label="截止日期" align="center" prop="deadline" />
      <el-table-column label="性别" align="center" prop="sexRequire" width="180">
        <template slot-scope="scope">
          {{ selectDictLabel(sexOptions, scope.row.sexRequire) }}
        </template>
      </el-table-column>
      <el-table-column label="截至时间" align="center" prop="deadline" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.deadline, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="薪资范围" align="center" width="180">
        <template slot-scope="scope">
          <span>{{ parseSalary(scope.row.salaryStart, scope.row.salaryEnd) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['hire:hire:edit']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
            v-hasPermi="['hire:hire:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改活动任务管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="50%" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="招聘标题">
              <el-input v-model="form.title" placeholder="请输入内容"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="职位描述">
              <el-input v-model="form.descInfo" type="textarea" placeholder="请输入内容"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="人数">
              <el-input v-model.number="form.hireNum" type="number" placeholder="请输入内容"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="性别要求">
              <el-select v-model="form.sexRequire" placeholder="请选择">
                <el-option v-for="dict in sexOptions" :key="dict.dictValue" :label="dict.dictLabel"
                  :value="dict.dictValue"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="岗位图片">
              <imageUpload v-model="form.jobImgUrl" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="截至时间" prop="endTime">
              <el-date-picker clearable size="small" v-model="form.deadline" type="date" value-format="yyyy-MM-dd"
                placeholder="选择截至时间">
              </el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row :gutter="12">
          <el-form-item label="薪资范围">
            <el-row :gutter="12">
              <el-col :span="5">
                <el-input v-model.number="form.salaryStart" type="number" placeholder="最低薪资"></el-input>
              </el-col>
              <el-col :span="2" style="text-align: center; line-height: 40px;">
                —
              </el-col>
              <el-col :span="5">
                <el-input v-model.number="form.salaryEnd" type="number" placeholder="最高薪资"></el-input>
              </el-col>
            </el-row>
          </el-form-item>
        </el-row>
        <el-row :gutter="12">
          <el-form-item label="年龄范围范围">
            <el-row :gutter="12">
              <el-col :span="5">
                <el-input v-model.number="form.ageStart" type="number" placeholder="最低年龄"></el-input>
              </el-col>
              <el-col :span="2" style="text-align: center; line-height: 40px;">
                —
              </el-col>
              <el-col :span="5">
                <el-input v-model.number="form.ageEnd" type="number" placeholder="最大年龄"></el-input>
              </el-col>
            </el-row>
          </el-form-item>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>

import ImageUpload from '@/components/ImageUpload';
import { listHire, getHire, addHire, updateHire, delHire, exportHire } from "@/api/hire/hire";
export default {
  name: "Hire",
  components: {
    ImageUpload
  },
  data() {
    return {
      // 遮罩层s
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 活动任务管理表格数据
      hireList: [
      ],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        title: null,
        sexRequire: null,
        deadline: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      },
      sexOptions: []
    };
  },
  created() {
    this.getList();
    this.getDicts("sys_user_sex").then(response => {
      this.sexOptions = response.data;
    });


  },
  methods: {
    parseSalary(salaryStart, salaryEnd) {
      return salaryStart + "-" + salaryEnd
    },
    /** 查询活动任务管理列表 */
    getList() {
      this.loading = true;
      listHire(this.queryParams).then(response => {
        this.hireList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },

    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        title: null,
        descInfo: null,
        hireNum: null,
        deadline: null,
        sexRequire: null,
        salaryStart: null,
        salaryEnd: null,
        createTime: null,
        ageStart: null,
        ageEnd: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加招聘信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getHire(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改招聘信息";
      });
    },

    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateHire(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addHire(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除招聘信息编号为"' + ids + '"的数据项?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(function () {
        return delHire(ids);
      }).then(() => {
        this.getList();
        this.msgSuccess("删除成功");
      })
    },
    /** 导出按钮操作 */
    // handleExport() {
    //   const queryParams = this.queryParams;
    //   this.$confirm('是否确认导出所有活动任务管理数据项?', "警告", {
    //     confirmButtonText: "确定",
    //     cancelButtonText: "取消",
    //     type: "warning"
    //   }).then(() => {
    //     this.exportLoading = true;
    //     return exportHire(queryParams);
    //   }).then(response => {
    //     this.download(response.msg);
    //     this.exportLoading = false;
    //   })
    // }
  }
};
</script>